package br.com.conhecimento.infra.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

/**
 * Classe responsavel por criptografia
 * 
 * @author Luis Garcia
 * @since 27/04/2013
 */
public class Criptography {

	/** Tipo de Mecanismo Hash */
	private static final String CRIPTO_TYPE = "MD5";
	
	/**
	 * Aplica hash sobre String
	 * @param value
	 * @return String
	 * @throws NoSuchAlgorithmException
	 */
	public static String encrypt(String value) throws NoSuchAlgorithmException {
		if (value == null) {
			throw new IllegalArgumentException("Chave para criptografia esta nula.");
		}
		MessageDigest messageDigest = MessageDigest.getInstance(CRIPTO_TYPE);
		BASE64Encoder encoder = new BASE64Encoder();
		byte[] hash = messageDigest.digest(value.getBytes());
		return encoder.encode(hash);
	}

}
